package config

import (
	"encoding/json"
	"gitee.com/showurl/ugly/sheets"
	"io/ioutil"
)

type All struct {
	Logic *Logic `json:"logic"`
	App   *App   `json:"app"`
}
type App struct {
	SheetFile string `json:"sheet_file"`
	SheetPage string `json:"sheet_page"`
	Sqlite    struct {
		Path string `json:"path"`
	} `json:"sqlite"`
}
type Logic struct {
	Speed float64 `json:"speed"`
}

var Config *All

func Init(path string) {
	file, err := ioutil.ReadFile(path)
	if err != nil {
		panic(err)
	}
	c := &All{}
	err = json.Unmarshal(file, c)
	if err != nil {
		panic(err)
	}
	Config = c
}

func (l *Logic) PianoSleepTime(t float64) int {
	return int(t * 36000 / l.Speed)
}

var sounds *[]string
var sheetType sheets.SheetType

func (a *App) GetSounds() ([]string, sheets.SheetType) {
	if sounds != nil {
		return *sounds, sheetType
	}
	bytes, err := ioutil.ReadFile(a.SheetFile)
	if err != nil {
		panic(err)
	}
	sheet := sheets.NewSheet()
	err = json.Unmarshal(bytes, &sheet)
	if err != nil {
		panic(err)
	}
	strings := sheet.Info[a.SheetPage]
	sounds = &strings
	sheetType = sheet.Type
	return strings, sheetType
}
